package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.model.LawBaseInfo;
import com.bojiu.lawsearch.lawsearch.model.SearchFactor;
import com.bojiu.lawsearch.lawsearch.model.UserCollect;
import com.bojiu.lawsearch.lawsearch.service.SearchService;
import com.bojiu.lawsearch.lawsearch.util.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: afdiao
 * @Date：2020/3/15 13:24
 * @Description:
 */
@Api(description = "用户查询相关接口")
@RestController
@RequestMapping("search")
@Slf4j
public class SearchController {

    @Autowired
    AuthorizationHandle authorizationHandle;

    @Autowired
    SearchService searchService;

    @ApiOperation(value = "查询接口", notes = "查询")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = LawBaseInfo.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 2000, message = "不是会员，请开通会员使用")
    })
    @PostMapping(produces = "application/json;charset=UTF-8")
    public JsonResult searchTitle(@ApiParam(name = "查询对象", value = "json格式", required = true) @RequestBody SearchFactor searchFactor, HttpServletRequest request) {
        log.debug("查询条件：{}", searchFactor);
        //判断用户权限
        JsonResult checkjsonResult = authorizationHandle.webHandler(request);
        if (!"200".equals(checkjsonResult.getCode())) {
            log.error("审核没有通过");
            return checkjsonResult;
        }
        int userId = Integer.parseInt(String.valueOf(checkjsonResult.getData()));
        if (!authorizationHandle.memberHandle(userId, true)) {
            log.error("还不是会员，无法查询");
            return new JsonResult(ResultCode.NO_MEMBER);
        }

        //开始查询
        List<LawBaseInfo> search = null;
        try {
            search = searchService.search(searchFactor);
            return new JsonResult(ResultCode.SUCCESS, search);
        } catch (Exception e) {
            log.error("error to search,error is :{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    @ApiOperation(value = "点击量", notes = "在查询结果页面点击大标题查看子条例时统计")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("clickcount/{id}")
    public JsonResult cilckCount(@ApiParam(name = "id", value = "大标题id", required = true) @PathVariable("id") int id, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        //TODO：在判断是否是会员，工程师的话可以随便查询
        searchService.clickCount(id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "收藏", notes = "用户在查询结果的条例页面收藏条例")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 2000, message = "不是会员，请开通会员使用"),
            @ApiResponse(code = 1200, message = "请求字段为空"),
            @ApiResponse(code = 1800, message = "该条例已经被收藏过，不能重复收藏")
    })
    @PostMapping("collect")
    public JsonResult collect(@ApiParam(name = "收藏条例的id以及添加的标签", value = "Json格式", required = true) @RequestBody UserCollect userCollect, Errors errors, HttpServletRequest request) {
        //判断用户权限
        JsonResult checkjsonResult = authorizationHandle.webHandler(request);
        if (!"200".equals(checkjsonResult.getCode())) {
            log.error("审核没有通过");
            return checkjsonResult;
        }
        int userId = Integer.parseInt(String.valueOf(checkjsonResult.getData()));
        if (!authorizationHandle.memberHandle(userId, true)) {
            log.error("还不是会员，无法查询");
            return new JsonResult(ResultCode.NO_MEMBER);
        }

        if (errors.hasErrors()) {
            JsonResult jsonResult = UtilTools.handleNullField(errors);
            return jsonResult;
        }
        return searchService.collectLabel(userCollect, userId);
    }

    @ApiOperation(value = "下载全文", notes = "下载全文")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 2000, message = "不是会员，请开通会员使用")
    })
    @GetMapping("getall/{titleId}")
    public JsonResult getAll(@PathVariable("titleId") int titleId, HttpServletRequest request) {
        //判断用户权限
        JsonResult checkjsonResult = authorizationHandle.webHandler(request);
        if (!"200".equals(checkjsonResult.getCode())) {
            log.error("审核没有通过");
            return checkjsonResult;
        }
        int userId = Integer.parseInt(String.valueOf(checkjsonResult.getData()));
        if (!authorizationHandle.memberHandle(userId, true)) {
            log.error("还不是会员，无法查询");
            return new JsonResult(ResultCode.NO_MEMBER);
        }

        JsonResult all = searchService.getAll(titleId);
        return all;
    }

}
